package com.usian.behavior.behavior.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.behavior.behavior.fegin.CollectionBehaciorFegin;
import com.usian.behavior.behavior.mapper.ApLikesBehaviorMapper;
import com.usian.behavior.behavior.mapper.ApReadBehaviorMapper;
import com.usian.behavior.behavior.mapper.ApUnlikeBehaviorMapper;
import com.usian.behavior.behavior.mapper.BehaviorEntryMapperMapper;
import com.usian.behavior.behavior.service.BehaviorEntryService;
import com.usian.model.article.ArticleInfoDto;
import com.usian.model.behavior.dto.ApUnlikesBehavior;
import com.usian.model.behavior.dto.CollectionBehaviorDto;
import com.usian.model.behavior.dto.ReadBehaviorDto;
import com.usian.model.behavior.pojo.ApBehaviorEntry;
import com.usian.model.behavior.pojo.ApLikesBehavior;
import com.usian.model.behavior.dto.LikesBehaviorDto;
import com.usian.model.behavior.pojo.ApReadBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/behavior/api/v1/")
public class BehaviorEntryContorller {

    @Autowired(required = false)
    private BehaviorEntryService behaviorEntryService;

    @Autowired
    private CollectionBehaciorFegin collectionBehaciorFegin;

    @Autowired
    private BehaviorEntryMapperMapper behaviorEntryMapperMapper;

    @Autowired
    private ApLikesBehaviorMapper apLikesBehaviorMapper;

    @Autowired
    private ApUnlikeBehaviorMapper apUnlikeBehaviorMapper;

    @Autowired
    private ApReadBehaviorMapper apReadBehaviorMapper;

    @RequestMapping("add")
    public void add(@RequestBody ApBehaviorEntry apBehaviorEntry){
        behaviorEntryService.add(apBehaviorEntry);
    }

    //点赞
    @RequestMapping("likes_behavior")
    public void likes_behavior(@RequestBody LikesBehaviorDto likesBehaviorDto){
        ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
        apLikesBehavior.setCreatedTime(new Date());
        apLikesBehavior.setType(likesBehaviorDto.getType());
        apLikesBehavior.setOperation(likesBehaviorDto.getOperation());
        apLikesBehavior.setArticleId(likesBehaviorDto.getEntry_id());
        ApUser user = AppThreadLocalUtils.getUser();

        //根据用户id进行查询行为表的id
        LambdaQueryWrapper<ApBehaviorEntry> q = new LambdaQueryWrapper<>();
        q.eq(ApBehaviorEntry::getEntryId,user.getId());
        ApBehaviorEntry apBehaviorEntry = behaviorEntryMapperMapper.selectOne(q);
        apLikesBehavior.setEntryId(apBehaviorEntry.getId());

        LambdaQueryWrapper<ApLikesBehavior> qq = new LambdaQueryWrapper<>();
        qq.eq(ApLikesBehavior::getArticleId,likesBehaviorDto.getEntry_id());
        qq.eq(ApLikesBehavior::getEntryId,apBehaviorEntry.getId());
        ApLikesBehavior apLikesBehavior1 = apLikesBehaviorMapper.selectOne(qq);
        if(apLikesBehavior1==null){
            apLikesBehaviorMapper.insert(apLikesBehavior);

        }else{
            apLikesBehavior.setId(apLikesBehavior1.getId());
            apLikesBehaviorMapper.updateById(apLikesBehavior);
        }
    }

    //不喜欢
    @RequestMapping("unlike_behavior")
    public void unlike_behavior(@RequestBody LikesBehaviorDto likesBehaviorDto){
        ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
        ApUser user = AppThreadLocalUtils.getUser();

        //根据用户id进行查询行为表的id
        LambdaQueryWrapper<ApBehaviorEntry> q = new LambdaQueryWrapper<>();
        q.eq(ApBehaviorEntry::getEntryId,user.getId());
        ApBehaviorEntry apBehaviorEntry = behaviorEntryMapperMapper.selectOne(q);
        apUnlikesBehavior.setEntryId(apBehaviorEntry.getId());

        //查询是否存在不喜欢
        LambdaQueryWrapper<ApUnlikesBehavior> qq = new LambdaQueryWrapper<>();
        qq.eq(ApUnlikesBehavior::getArticleId,likesBehaviorDto.getArticleId());
        qq.eq(ApUnlikesBehavior::getEntryId,apBehaviorEntry.getId());
        ApUnlikesBehavior apUnlikesBehavior1 = apUnlikeBehaviorMapper.selectOne(qq);

        apUnlikesBehavior.setArticleId(likesBehaviorDto.getArticleId());
        apUnlikesBehavior.setCreatedTime(new Date());
        apUnlikesBehavior.setType(likesBehaviorDto.getType());
        if(apUnlikesBehavior1==null){
            apUnlikeBehaviorMapper.insert(apUnlikesBehavior);
        }else{
            apUnlikesBehavior.setId(apUnlikesBehavior1.getId());
            apUnlikeBehaviorMapper.updateById(apUnlikesBehavior);
        }
    }

    //收藏
    @PostMapping("collection_behavior")
    public void collection_behavior(@RequestBody CollectionBehaviorDto collectionBehaviorDto){
        ApUser user = AppThreadLocalUtils.getUser();

        //根据用户id进行查询行为表的id
        LambdaQueryWrapper<ApBehaviorEntry> q = new LambdaQueryWrapper<>();
        q.eq(ApBehaviorEntry::getEntryId,user.getId());
        ApBehaviorEntry apBehaviorEntry = behaviorEntryMapperMapper.selectOne(q);
        Integer id = apBehaviorEntry.getId();
        collectionBehaviorDto.setEntry_id(id);

        collectionBehaciorFegin.addApCollctionBehacior(collectionBehaviorDto);
    }

    //阅读
    @PostMapping("read_behavior")
    public void read_behavior(@RequestBody ReadBehaviorDto readBehaviorDto){
        ApUser user = AppThreadLocalUtils.getUser();
        //根据用户id进行查询行为表的id
        LambdaQueryWrapper<ApBehaviorEntry> q = new LambdaQueryWrapper<>();
        q.eq(ApBehaviorEntry::getEntryId,user.getId());
        ApBehaviorEntry apBehaviorEntry = behaviorEntryMapperMapper.selectOne(q);

        ApReadBehavior apReadBehavior = new ApReadBehavior();

        apReadBehavior.setEntryId(apBehaviorEntry.getId());
        apReadBehavior.setCreatedTime(new Date());
        apReadBehavior.setArticleId(readBehaviorDto.getArticleId());
        apReadBehavior.setUpdatedTime(new Date());
        apReadBehavior.setCount(1);
        apReadBehavior.setLoadDuration(readBehaviorDto.getLoadDuration());
        apReadBehavior.setPercentage(readBehaviorDto.getPercentage());
        apReadBehavior.setReadDuration(readBehaviorDto.getReadDuration());
        //查询是否存在阅读
        LambdaQueryWrapper<ApReadBehavior> qq = new LambdaQueryWrapper<>();
        qq.eq(ApReadBehavior::getArticleId,readBehaviorDto.getArticleId());
        qq.eq(ApReadBehavior::getEntryId,apBehaviorEntry.getId());
        ApReadBehavior apReadBehavior1 = apReadBehaviorMapper.selectOne(qq);
        if(apReadBehavior1==null){
            apReadBehaviorMapper.insert(apReadBehavior);
        }else{
            apReadBehavior.setId(apReadBehavior1.getId());
            Integer count = apReadBehavior1.getCount();
            count++;
            apReadBehavior.setCount(count);
            apReadBehavior.setUpdatedTime(new Date());
            apReadBehaviorMapper.updateById(apReadBehavior);
        }
    }

    //阅读
    @PostMapping("findById")
    public ApBehaviorEntry read_behavior(@RequestBody ArticleInfoDto dto){
        LambdaQueryWrapper<ApBehaviorEntry> q = new LambdaQueryWrapper<>();
        q.eq(ApBehaviorEntry::getEntryId,dto.getEntryId());
        ApBehaviorEntry apBehaviorEntry = behaviorEntryMapperMapper.selectOne(q);
        return apBehaviorEntry;
    }
}
